# This file is part of Sonedyan.
#
# Sonedyan is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation;
# either version 3 of the License, or (at your option) any
# later version.
#
# Sonedyan is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public.
# If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2009-2012 Jimmy Dubuisson <jimmy.dubuisson@gmail.com>

#
#
# compute coefficients of the correlation matrix (slow)
#

#from scipy.stats.stats import pearsonr
from scipy.stats.stats import spearmanr

# load the normalized filtered 1grams time series
fd1 = open("normalized-filtered-1grams.txt", "r")
fd2 = open("correlation-matrix.txt", "w")

line = fd1.readline().strip()
counter = 0
record = {}

print "Loading time series..."

while line:
        elements = line.split(',')
        record[counter] = elements[1:]
        line = fd1.readline().strip()
	counter += 1

fd1.close()

print "Loaded"
print "Now computing correlations..."

counter2 = 0

for i in range(0, counter):
	for j in range(0, i + 1):
		if (i == j):
			corr = float(1)
		else:
			corr, pvalue = spearmanr(record[i], record[j])
		
		fd2.write(str(corr) + " ")	
		counter2 += 1

print str(counter2) + " correlation indices computed successfully!"

fd2.close()
